package com.dalianpai.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.dalianpai.common.exception.BizCodeEnume;
import com.dalianpai.common.to.SocialUser;
import com.dalianpai.common.utils.RRException;
import com.dalianpai.gulimall.member.feign.CouponFeignService;
import com.dalianpai.gulimall.member.vo.UserLoginVo;
import com.dalianpai.gulimall.member.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dalianpai.gulimall.member.entity.MemberEntity;
import com.dalianpai.gulimall.member.service.MemberService;
import com.dalianpai.common.utils.PageUtils;
import com.dalianpai.common.utils.R;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 会员
 *
 * @author dalianpai
 * @email dalianpai@163.com
 * @date 2020-05-25 22:11:22
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

//    @GetMapping("/setSession")
//    public R test1(HttpServletRequest request, HttpServletResponse response){
//        request.getSession().setAttribute("wgr","123");
//        Cookie C =new Cookie("123","323");
//        C.setMaxAge(2000);
//        response.addCookie(C);
//        return R.ok();
//    }
//
//    @GetMapping("/getSession")
//    public R test2(HttpServletRequest request){
//        System.out.println(request.getSession().getId());
//        return R.ok();
//    }

    @GetMapping("/getCoupon")
    public R test(){
        return couponFeignService.membercoupons();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping ("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity entity = memberService.login(socialUser);
        if(entity !=null){
            return R.ok().setData(entity);
        }else{
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
        }
    }

    @PostMapping ("/regist")
    public R regist(@RequestBody UserRegistVo vo){
        try{
            memberService.regist(vo);
        }catch (RRException e){
            System.out.println(e.getMsg());
            return R.error(e.getCode(),e.getMsg());
        }
        return R.ok();
    }

    @PostMapping ("/login")
    public R login(@RequestBody UserLoginVo vo){
            MemberEntity entity = memberService.login(vo);
            if(entity !=null){
                return R.ok().setData(entity);
            }else{
                return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
            }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
